<?php

class adminAjax extends APPS_Controller {
	private $tpl;
	
  	function __construct() {
		parent::__construct();
		$this->load->model('adminModel');
		
		$this->load->library('cezpdf');
		$this->site['assets']	= $this->site['site'] .'assets/admin/';
		$this->smarty->assign('site', $this->site);	
	}
	
	function index() {
		//$this->login();
	}

	function login(){
		$response 	= "0";
		$post 		= $this->_post();
		$row 		= $this->adminModel->getLogin($post);
		if($row) {
			if ($row['state']== "1"){
				$ip = $_SERVER['REMOTE_ADDR'];
				$this->data['user_name'] = $row['user_name'];
				$this->data['user_id'] = $row['user_id'];
				$this->data['acl_id'] = $row['acl_id'];
				$this->data['is_auth'] = TRUE;
				$this->session->set_userdata('is_auth', TRUE);
				$this->session->set_userdata('data', $this->data);


				$sql = "UPDATE app_user set user_last_login = now(), user_last_ip = '$ip'
						WHERE user_id = $row[user_id]";
				$this->db->query($sql);

				//$response = base_url()."admin/dashboard" ;
				$response = base_url()."admin/pendaftaran" ;
			} else {
				$response = "1";
			}
		}
		echo $response;
	}

    function _post() {
		$post = array();
		foreach($_POST as $k=>$v) {
			if(is_array($_POST[$k])) {
				$index = $this->input->post($k);
				foreach($index as $x=>$y) {
					$value 		  = $y ? $y : null;
					$post[$k][$x] = addslashes($value);
				}
			}
			else {
				$value 	  = $this->input->post($k) ? $this->input->post($k) : null;
				$post[$k] = addslashes($value);
			}
		}
		return $post;
	}
	
	function contentSorting($order='contentID', $sort='asc', $page=1, $limit=10){
		$offset = $this->setPaging($page, $limit, $this->adminModel->getContent());
		$this->data['rows']	 = $this->adminModel->getContent($order, $sort, $limit, $offset);
		$this->data['order'] = $order;
		$this->data['sort']  = $sort;

		$fields = array('contentID', 'title', 'state', 'created');
		foreach ($fields as $key=>$value) {
			if (strtolower($order) === strtolower($value) ) {
				if ($sort=="none") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="desc") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="asc") {
				   $this->data['sort_'.$value]  = "desc";
				} else {
				   $this->data['sort_'.$value]  = $sort;
				}
			} else {
				$this->data['sort_'.$value]  = "none";
			}
		}
		
		$this->smarty->assign('data', $this->data);
		$this->smarty->display('admin/pages/content_table.tpl');
	}	
	

	function contentPublishing(){
		$response = "NONE";
		$id       = $this->input->post('id');
		$publish  = $this->input->post('publish');

 		if ($id > 0 ){
			$state = ($publish == "1") ? "0" : "1";
	
			$this->adminModel->setState($id, $state);
			$response   = $state;
		}
		echo $response;
    }

	function contentUpdate(){
		$contentID		= $this->input->post('contentID');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$title 			= addslashes($this->input->post('title'));
		$fulltext		= addslashes($this->input->post('fulltext'));
		//$state 			= $this->input->post('state');
		$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$slideshow 		= (strlen($this->input->post('slideshow')) > 0) ? $this->input->post('slideshow') : "NULL";

		$image_1 		= $_FILES["img_1"];
		$image_2 		= $_FILES["img_2"];
		$image_3 		= $_FILES["img_3"];
		$image_path = "./assets/images/";
		$update 		= "";
		$sql 			= "";
		$insert_images 			= "";
		$insert_image_1 		= "";
		$insert_image_2 		= "";
		$insert_image_3 		= "";

		
		//echo $contentID;
		if ($contentID !=  "0") {
			$sql = "UPDATE content SET 
						title = '$title',
						state = $state,
						introtext = '". $this->getIntro($fulltext) ."',
						`fulltext` = '$fulltext',
						slideshow = $slideshow 
					WHERE contentid = $contentID ";
		} else {
			$sql = "INSERT INTO content (
							userID, title, categoryID,
							introtext, `fulltext`, slideshow, 
							state, created)
					VALUES (
							'1', '$title', '13',
							'".$this->getIntro($fulltext) ."', '$fulltext', $slideshow,
							$state, now())";
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			if ($contentID==0)  $contentID = $this->db->insert_id();
		}
		if($image_1!=null){
			move_uploaded_file($image_1["tmp_name"], $image_path.$image_1["name"]);
			if (strlen( $image_1["name"]) > 0 ) {
				$img = "assets/images/". $image_1["name"];
				$sql = "delete from content_img 
						WHERE contentid = $contentID and img_type like '%headline%'";
					
				$this->db->query($sql);	
				
				$img_type = 'content, headline';
				if ($slideshow == "1") $img_type = 'content, headline, slideshow';
				
				$sql = "INSERT INTO content_img (contentID, img, img_type, slideshow)
						VALUES ('$contentID', '$img', '$img_type', $slideshow)";
				$this->db->query($sql);	
					
			}
		}
		if($image_2!=null){
			move_uploaded_file($image_2["tmp_name"], $image_path.$image_2["name"]);
			if (strlen( $image_2["name"]) > 0 ) {
				$img = "assets/images/". $image_2["name"];
				$sql = "delete from content_img 
						WHERE contentid = $contentID and img_type like '%img1%'";
					
				$this->db->query($sql);	
				
				$img_type = 'content, img1';
				$sql = "INSERT INTO content_img (contentID, img, img_type)
						VALUES ('$contentID', '$img', '$img_type')";
				$this->db->query($sql);	
			}
		}
		if($image_3!=null){
			move_uploaded_file($image_3["tmp_name"], $image_path.$image_3["name"]);
			if (strlen( $image_3["name"]) > 0 ) {
				$img = "assets/images/". $image_3["name"];
				$sql = "delete from content_img 
						WHERE contentid = $contentID and img_type like '%img2%'";
					
				$this->db->query($sql);	
				
				$img_type = 'content, img2';
				$sql = "INSERT INTO content_img (contentID, img, img_type)
						VALUES ('$contentID', '$img', '$img_type')";
				$this->db->query($sql);	
			}
		}

		redirect("/admin/contentForm/$contentID/$order/$sort/$page/$limit/SAVED");
	}


	function fasilitasSorting($order='contentID', $sort='asc', $page=1, $limit=10){
		$offset = $this->setPaging($page, $limit, $this->adminModel->getFasilitas());
		$this->data['rows']	 = $this->adminModel->getFasilitas($order, $sort, $limit, $offset);
		$this->data['order'] = $order;
		$this->data['sort']  = $sort;

		$fields = array('contentID', 'title', 'state', 'created');
		foreach ($fields as $key=>$value) {
			if (strtolower($order) === strtolower($value) ) {
				if ($sort=="none") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="desc") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="asc") {
				   $this->data['sort_'.$value]  = "desc";
				} else {
				   $this->data['sort_'.$value]  = $sort;
				}
			} else {
				$this->data['sort_'.$value]  = "none";
			}
		}
		
		$this->smarty->assign('data', $this->data);
		$this->smarty->display('admin/pages/fasilitas_table.tpl');
	}	
	
	function userSorting($order='user_id', $sort='asc', $page=1, $limit=10){
		$offset = $this->setPaging($page, $limit, $this->adminModel->getUser());
		$this->data['rows']	 = $this->adminModel->getUser($order, $sort, $limit, $offset);
		$this->data['order'] = $order;
		$this->data['sort']  = $sort;

		$fields = array('user_id', 'user_name', 'name', 'state', 'created');
		foreach ($fields as $key=>$value) {
			if (strtolower($order) === strtolower($value) ) {
				if ($sort=="none") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="desc") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="asc") {
				   $this->data['sort_'.$value]  = "desc";
				} else {
				   $this->data['sort_'.$value]  = $sort;
				}
			} else {
				$this->data['sort_'.$value]  = "none";
			}
		}
		
		$this->smarty->assign('data', $this->data);
		$this->smarty->display('admin/pages/user_table.tpl');
	}		


	function userPublishing(){
		$response = "NONE";
		$id       = $this->input->post('user_id');
		$publish  = $this->input->post('publish');

 		if ($id > 0 ){
			$state = ($publish == "1") ? "0" : "1";
	
			$this->adminModel->setUserState($id, $state);
			$response   = $state;
		}
		echo $response;
    }
	
	function fasilitasUpdate(){
		$contentID		= $this->input->post('contentID');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$title 			= addslashes($this->input->post('title'));
		$fulltext		= addslashes($this->input->post('fulltext'));
		//$state 			= $this->input->post('state');
		$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$slideshow 		= (strlen($this->input->post('slideshow')) > 0) ? $this->input->post('slideshow') : "NULL";

		$image_1 		= $_FILES["img_1"];
		$image_path = "./assets/images/";
		$update 		= "";
		$sql 			= "";
		$insert_images 			= "";
		$insert_image_1 		= "";
		$insert_image_2 		= "";
		$insert_image_3 		= "";

		
		//echo $contentID;
		if ($contentID !=  "0") {
			$sql = "UPDATE content SET 
						title = '$title',
						state = $state,
						introtext = '". $this->getIntro($fulltext) ."',
						`fulltext` = '$fulltext',
						slideshow = $slideshow 
					WHERE contentid = $contentID ";
		} else {
			$sql = "INSERT INTO content (
							userID, title, categoryID,
							introtext, `fulltext`, slideshow, 
							state, created)
					VALUES (
							'1', '$title', '7',
							'".$this->getIntro($fulltext) ."', '$fulltext', $slideshow,
							$state, now())";
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			if ($contentID==0)  $contentID = $this->db->insert_id();
		}
		if($image_1!=null){
			move_uploaded_file($image_1["tmp_name"], $image_path.$image_1["name"]);
			if (strlen( $image_1["name"]) > 0 ) {
				$img = "assets/images/". $image_1["name"];
				$sql = "delete from content_img 
						WHERE contentid = $contentID and img_type like '%headline%'";
					
				$this->db->query($sql);	
				
				$img_type = 'content, headline';
				if ($slideshow == "1") $img_type = 'content, headline, slideshow';
				
				$sql = "INSERT INTO content_img (contentID, img, img_type, slideshow)
						VALUES ('$contentID', '$img', '$img_type', $slideshow)";
				$this->db->query($sql);	
					
			}
		}
		

		redirect("/admin/fasilitasForm/$contentID/$order/$sort/$page/$limit/SAVED");
	}
	function pejabatUpdate(){
		$contentID		= $this->input->post('ID');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$nama			= addslashes($this->input->post('nama'));
		$jabatan 		= addslashes($this->input->post('jabatan'));
		$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$sql 			= "";

		//echo $contentID;
		if ($contentID !=  "0") {
			$sql = "UPDATE pejabat  SET 
						nama  = '$nama',
						jabatan = '$jabatan',
						state = $state
					WHERE id = $contentID ";
		} else {
			$sql = "INSERT INTO pejabat (
							 nama, jabatan,
							state, created)
					VALUES (
							'1', '$nama', '$jabatan',
							$state, now())";
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			if ($contentID=="0")  $contentID = $this->db->insert_id();
		}

		redirect("/admin/pejabatForm/$contentID/$order/$sort/$page/$limit/SAVED");
	}	
	
	function pengajarUpdate(){
		$contentID		= $this->input->post('ID');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$nama			= addslashes($this->input->post('nama'));
		$nippns			= addslashes($this->input->post('nippns'));
		$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$sql 			= "";

		//echo $contentID;
		if ($contentID !=  "0") {
			$sql = "UPDATE dosen  SET 
						nama  = '$nama',
						state = $state,
						nippns = '$nippns'
					WHERE id = $contentID ";
		} else {
			$sql = "INSERT INTO dosen (
							userID, nama, nippns,
							state, created)
					VALUES (
							'1', '$nama', '$nippns',
							$state, now())";
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			if ($contentID=="0")  $contentID = $this->db->insert_id();
		}

		redirect("/admin/pengajarForm/$contentID/$order/$sort/$page/$limit/SAVED");
	}

	function sectionUpdate_ori(){
		$contentID		= $this->input->post('sectionID');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$title 			= addslashes($this->input->post('title'));
		$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$sql 			= "";
		$alias 			= "'". str_replace(" ", "", strtolower($title)) ."'";

		if ($contentID !=  "0") {
			$sql = "UPDATE section  SET 
						title  = '$title',
						alias = $alias,
						state = $state
					WHERE sectionid = $contentID ";
		} else {
			$sql = "INSERT INTO section  (title, alias, state)
					VALUES ('$title', $alias, $state)";
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			if ($contentID=="0")  $contentID = $this->db->insert_id();
		}

		redirect("/admin/sectionForm/$contentID/$order/$sort/$page/$limit/SAVED");
	}

	function sectionUpdate(){
		$ID 	= $this->input->post('sectionID');
		$title 	= addslashes($this->input->post('title'));
		$state 	= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$alias 	= "'". str_replace(" ", "", strtolower($title)) ."'";
		$sql 	= "";

		if ($ID !=  "0") {
			$sql = "UPDATE section  SET 
						title  = '$title',
						alias = $alias,
						state = $state
					WHERE sectionid = $ID ";
		} else {
			$sql = "INSERT INTO section  (title, alias, state)
					VALUES ('$title', $alias, $state)";
		}
		
		if (strlen($sql) > 0 ) {
			$this->db->query($sql);			
			$response = "SAVED";
		}
		echo $response;
	}
	
	function categoryUpdate(){
		$contentID		= $this->input->post('categoryID');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$title 			= addslashes($this->input->post('title'));
		$sectionid 		= addslashes($this->input->post('sectionid'));
		$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$sql 			= "";
		$alias 			= "'". str_replace(" ", "", strtolower($title)) ."'";
		//echo $contentID;
		if ($contentID !=  "0") {
			$sql = "UPDATE category  SET 
						title  = '$title',
						alias = $alias,
						sectionid  = '$sectionid',
						state = $state
					WHERE categoryid = $contentID ";
		} else {
			$sql = "INSERT INTO category (sectionid, title, alias, state)
					VALUES ('$sectionid', '$title',$alias,  $state)";
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			if ($contentID=="0")  $contentID = $this->db->insert_id();
		}

		redirect("/admin/categoryForm/$contentID/$order/$sort/$page/$limit/SAVED");
	}

	
	function userUpdate(){
		$contentID		= $this->input->post('user_id');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$user_name 		= addslashes($this->input->post('user_name'));
		$name 			= addslashes($this->input->post('name'));
		$pass			= addslashes($this->input->post('pass'));
		$acl_id 		= addslashes($this->input->post('acl_id'));
		$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$sql 			= "";

		//echo $contentID;
		if ($contentID !=  "0") {
			$sql = "UPDATE app_user   SET 
						user_name  = '$user_name',
						name = '$name',
						pass  = '$pass',
						acl_id = '$acl_id',
						state = $state,
						created = now()
					WHERE user_id = $contentID ";
		} else {
			$sql = "INSERT INTO app_user  (user_id, user_name, name, acl_id, pass, state, created)
					VALUES ('$contentID', '$user_name', '$name','$acl_id', '$pass',  $state, now())";
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			if ($contentID=="0")  $contentID = $this->db->insert_id();
		}

		redirect("/admin/userForm/$contentID/$order/$sort/$page/$limit/SAVED");
	}
	
	
	function beritaUpdate(){
		$contentID		= $this->input->post('contentID');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$title 			= addslashes($this->input->post('title'));
		$state 			= $this->input->post('state');
		$fulltext		= addslashes($this->input->post('fulltext'));
		$slideshow 		= (strlen($this->input->post('isSlideshow')) > 0) ? $this->input->post('isSlideshow') : "NULL";
		
		print_r($slideshow);
/*		
		$image_1 		= $_FILES["img_1"];
		$image_2 		= $_FILES["img_2"];
		$image_3 		= $_FILES["img_3"];
		$image_4 		= $_FILES["img_4"];
		$image_5 		= $_FILES["img_5"];
*/
		$image_path = "./images/";
		$update 		= "";
		$sql 			= "";
		$insert_images 			= "";
		$insert_image_1 		= "";
		$insert_image_2 		= "";
		$insert_image_3 		= "";
		$insert_image_4 		= "";
		$insert_image_5 		= "";
/*		
		if($image_1!=null){
			move_uploaded_file($image_1["tmp_name"], $image_path.$image_1["name"]);
			if (strlen( $image_1["name"]) > 0 ) {
				$update 	.= " images = '" .$image_1["name"]. "', img_1 = '" .$image_1["name"]. "', ";
				$insert_images		= $image_1["name"];
				$insert_image_1	= $image_1["name"];
			}
		}
		if($image_2!=null){
			move_uploaded_file($image_2["tmp_name"], $image_path.$image_2["name"]);
			if (strlen( $image_2["name"]) > 0 ) {
				$update 	.= " img_2 = '" .$image_2["name"]. "', ";
				$insert_image_2	= $image_2["name"];
			}
		}
		if($image_3!=null){
			move_uploaded_file($image_3["tmp_name"], $image_path.$image_3["name"]);
			if (strlen( $image_3["name"]) > 0 ) {
				$update 	.= " img_3 = '" .$image_3["name"]. "', ";
				$insert_image_3	= $image_3["name"];
			}
		}
		if($image_4!=null){
			move_uploaded_file($image_4["tmp_name"], $image_path.$image_4["name"]);
			if (strlen( $image_4["name"]) > 0 ) {
				$update 	.= " img_4 = '" .$image_4["name"]. "', ";
				$insert_image_4	= $image_4["name"];
			}
		}
		if($image_5!=null){
			move_uploaded_file($image_5["tmp_name"], $image_path.$image_5["name"]);
			if (strlen( $image_5["name"]) > 0 ) {
				$update 	.= " img_5 = '" .$image_5["name"]. "', ";
				$insert_image_5	= $image_5["name"];
			}
		}
*/
		if (strlen( $update) > 0 ) {
			$update = ",".substr( $update, 0, strlen($update) - 2);
		}
		
		if ($contentID > 0) {
			$sql = "UPDATE content SET 
						title = '$title',
						state = $state,
						introtext = '". $this->getIntro($fulltext) ."',
						fulltext = '$fulltext',
						$update 
					  WHERE contentid = $contentID ";
		} else {
			$sql = "INSERT INTO content (
							userID, title, categoryID,
							introtext, fulltext, slideshow, 
							state, created)
						VALUES (
							'1', '$title', '13',
							'".$this->getIntro($fulltext) ."', '$fulltext', $slideshow,
							'$state', now())";
		}
		
		die();
		
		if (strlen($sql)> 0 ) {
			$this->result($sql, "row");
			//if ($id==0)  $id = $this->db->insert_id();
		}

		redirect("/admin/beritaEdit/$contentID/$order/$sort/$page/$limit/SAVED");
	}
	
	function beritaSorting($order='contentID', $sort='asc', $page=1, $limit=10){
		$offset = $this->setPaging($page, $limit, $this->adminModel->getBerita());
		
		//$this->data['rows'] 	= $this->adminModel->getAds(null, null, $limit, $offset);

		$this->data['rows']	 = $this->adminModel->getBerita($order, $sort, $limit, $offset);
		$this->data['order'] = $order;
		$this->data['sort']  = $sort;

		$fields = array('contentID', 'title', 'state', 'created');
      
		foreach ($fields as $key=>$value) {
			if (strtolower($order) === strtolower($value) ) {
				if ($sort=="none") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="desc") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="asc") {
				   $this->data['sort_'.$value]  = "desc";
				} else {
				   $this->data['sort_'.$value]  = $sort;
				}
			} else {
				$this->data['sort_'.$value]  = "none";
			}
		}
		
		$this->smarty->assign('data', $this->data);
		$this->smarty->display('admin/dashboard/berita_table.tpl');
		//echo $this->load->view('berita_table',$this->data, true);
	}
	
	function beritaPublishing(){
		$response = "NONE";
		$id       = $this->input->post('id');
		$publish  = $this->input->post('publish');

 		if ($id > 0 ){
			$state = $publish == "1" ? "0" : "1";
	
			$this->adminModel->setState($id, $state);
			$response   = $state;
			
		}
		echo $response;
    }

	function getIntro($field) {
		// convert line breaks/paragraphs
		// strip all remaining tags
		$field = strip_tags($field);
     
		// try and return the first paragraph, if I can't, return all of it
		$paragraphs = explode("\n\n", trim($field));
		 
		if(count($paragraphs) > 1) {
			 return nl2br(trim($paragraphs[0]));
		} else {
			 return $field;
		}
	}

	function test() {
		echo 'test';
	}
	
	function kuliahUpdate(){
		$id				= $this->input->post('ID');
		$idnew			= $this->input->post('IDnew');

		$nama			= addslashes($this->input->post('nama'));
		$jenis			= $this->input->post('jenis');
		$semester		= $this->input->post('semester');
		$sks			= $this->input->post('sks');

		$sql 			= "";

		if ($id !=  "0") {
			$sql = "UPDATE makul SET 
						nama  = '$nama',
						jenis  = '$jenis',
						semester  = '$semester',
						sks   = '$sks'
					WHERE id = '$id' ";
		} else {
	
			$sql = "INSERT INTO makul (
							nama, jenis, semester, sks, id)
					VALUES (
							'$nama', '$sks', '$semester', '$jenis', '$idnew')";
			$id = $idnew;
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			$response = "SAVED";
		}

		echo $response;
	}

	/* mi2208 05:32 */
	function kuliahUpdate_nonAjax(){
		$id				= $this->input->post('ID');
		$idnew			= $this->input->post('IDnew');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$nama			= addslashes($this->input->post('nama'));
		$sks			= $this->input->post('sks');
		$semester		= $this->input->post('semester');
		$jenis			= $this->input->post('jenis');
		//$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$sql 			= "";

		if ($id !=  "0") {
			$sql = "UPDATE makul SET 
						nama  = '$nama',
						sks   = '$sks',
						semester  = '$semester',
						jenis  = '$jenis'
					WHERE id = '$id' ";
		} else {
	
			$sql = "INSERT INTO makul (
							nama, sks, semester, jenis, id)
					VALUES (
							'$nama', '$sks', '$semester', '$jenis', '$idnew')";
			$id = $idnew;
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			if ($id=="0")  $id = $this->db->insert_id();
		}

		redirect("/admin/kuliahForm/$id/$order/$sort/$page/$limit/SAVED");
	}

	function silabusUpdate(){
		$id 			= $this->input->post('id');
		$kode			= $this->input->post('kode');
		$kodenew		= $this->input->post('kodenew');
		$jurusan		= $this->input->post('jurusan');
		$mataKuliah		= addslashes($this->input->post('mataKuliah'));
		$jenis			= $this->input->post('jenis');
		$semester		= $this->input->post('semester');
		$sks			= $this->input->post('sks');
		
		$sql 			= "";
		
		if ($id !=  "0") {
			$sql = "UPDATE silabus SET 
						kode  = '$kode'
						mataKuliah  = '$mataKuliah',
						jenis  = '$jenis',
						semester  = '$semester',
						sks   = '$sks',
					WHERE id = '$id' ";
		} else {
	
			$sql = "INSERT INTO silabus (
							kode, mataKuliah, jenis, semester, sks, jurusanID )
					VALUES (
							'$kodenew', '$mataKuliah', '$jenis', '$semester', '$sks', '3' )";
			$kode = $kodenew;
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			$response = "SAVED";
		}
		
		echo $response;
	}

	/* ilham silabus 220810 21:12 */
	function silabusUpdate_nonAjax(){
		$kode			= $this->input->post('kode');
		$kodenew		= $this->input->post('kodenew');
		$order 			= $this->input->post('order');
		$sort 			= $this->input->post('sort');
		$page			= $this->input->post('page');
		$limit			= $this->input->post('limit');
		$mataKuliah		= addslashes($this->input->post('mataKuliah'));
		$sks			= $this->input->post('sks');
		$semester		= $this->input->post('semester');
		$jenis			= $this->input->post('jenis');
		//$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$sql 			= "";

		if ($id !=  "0") {
			$sql = "UPDATE silabus SET 
						mataKuliah  = '$mataKuliah',
						sks   = '$sks',
						semester  = '$semester',
						jenis  = '$jenis',
						kode  = '$kode'
					WHERE id = '$id' ";
		} else {
	
			$sql = "INSERT INTO makul (
							mataKuliah, sks, semester, jenis, kode)
					VALUES (
							'$mataKuliah', '$sks', '$semester', '$jenis', '$kodenew')";
			$kode = $kodenew;
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			//if ($id=="0")  $id = $this->db->insert_id();
		}

		redirect("/admin/silabusForm/$id/$order/$sort/$page/$limit/SAVED");
	}
	/* end here */		
	
	/* Daftar Mahasiswa 220810 22:16 */
	function mahasiswaUpdate(){
		$id				= $this->input->post('ID');
		$idnew			= $this->input->post('IDnew');
		$nama			= addslashes($this->input->post('nama'));
		$jurusan		= $this->input->post('jurusan');
		$angkatan		= $this->input->post('angkatan');
		$kelas			= $this->input->post('kelas');
		
		$sql 			= "";

		if ($id !=  "0") {
			$sql = "UPDATE mahasiswa SET 
						nama  	  = '$nama',
						angkatan  = '$angkatan',
						kelas     = '$kelas'
					WHERE id = '$id' ";
		} else {
	
			$sql = "INSERT INTO mahasiswa (
							nama, angkatan, kelas, id)
					VALUES (
							'$nama', '$angkatan', '$kelas', '$idnew')";
			$id = $idnew;
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			$response = "SAVED";
		}
		
		echo $response;
	}
	/* end here Daftar Mahasiswa */

	/* Kelulusan 230810 01:55 */
	function lulusUpdate(){
		$id				= $this->input->post('ID');
		$idnew			= $this->input->post('IDnew');
		$nama			= addslashes($this->input->post('nama'));
		$jurusan		= $this->input->post('jurusan');
		
		$sql 			= "";

		if ($id !=  "0") {
			$sql = "UPDATE alumni SET 
						nama  	  = '$nama'
					WHERE id = '$id' ";
		} else {
	
			$sql = "INSERT INTO alumni (
							nama, id)
					VALUES (
							'$nama', '$idnew')";
			$id = $idnew;
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			$response = "SAVED";
		}
		
		echo $response;
	}
	
	/* end here kelulusan */

	/* Alummni 230810 02:34 */
	function alumniUpdate(){
		$id				= $this->input->post('ID');
		$idnew			= $this->input->post('IDnew');
		$nama			= addslashes($this->input->post('nama'));
		$jurusan		= $this->input->post('jurusan');
		$angkatan		= $this->input->post('angkatan');
		$lulus			= $this->input->post('lulus');

		$sql 			= "";

		if ($id !=  "0") {
			$sql = "UPDATE alumni SET 
						nama  	  = '$nama',
						angkatan  = '$angkatan',
						lulus     = '$lulus'
					WHERE id = '$id' ";
		} else {
	
			$sql = "INSERT INTO alumni(
							nama, angkatan, lulus, id)
					VALUES (
							'$nama', '$angkatan', '$lulus', '$idnew')";
			$id = $idnew;
		}
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			$response = "SAVED";
		}
		
		echo $response;
	}	
	/* end here alumni */

	/* kalenderUpdate */
	function kalenderUpdate(){
		$id				= $this->input->post('id');
		$title 			= addslashes($this->input->post('title'));
		$event			= addslashes($this->input->post('event'));
		$state 			= (strlen($this->input->post('state')) > 0) ? $this->input->post('state') : "NULL";
		$dateFrom		= $this->input->post('dateFrom');
		$dateTo			= $this->input->post('dateTo');
		//$file			=
		
		$sql			= "";
		
		/*
		$image_1 		= $_FILES["img_1"];
		$image_path = "./assets/images/";
		$update 		= "";
		$sql 			= "";
		$insert_images 			= "";
		$insert_image_1 		= "";
		$insert_image_2 		= "";
		$insert_image_3 		= "";
		*/	
			
		if ($id !=  "0") {
			$sql = "UPDATE kalender SET 
						title = '$title', 
						event = '$event',
						state = '$state',
						dateFrom = '$dateFrom',
						dateTo = '$dateTo'
					WHERE id = '$id' ";
		} else {
			$sql = "INSERT INTO kalender (
							title, event, state, dateFrom, dateTo)
					VALUES (
							'$title', '$event', '$state', '$dateFrom', '$dateTo'
							)";
		}
		
		/*
		if($image_1!=null){
			move_uploaded_file($image_1["tmp_name"], $image_path.$image_1["name"]);
			if (strlen( $image_1["name"]) > 0 ) {
				$img = "assets/images/". $image_1["name"];
				$sql = "delete from content_img 
						WHERE contentid = $contentID and img_type like '%headline%'";
					
				$this->db->query($sql);	
				
				$img_type = 'content, headline';
				if ($slideshow == "1") $img_type = 'content, headline, slideshow';
				
				$sql = "INSERT INTO content_img (contentID, img, img_type, slideshow)
						VALUES ('$contentID', '$img', '$img_type', $slideshow)";
					
			}
		}
		*/
		
		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			$response = "SAVED";
		}
		
		echo $response;
	}
	/* end here kalender akademik */

	/* Perpustakaan */
	function pustakaUpdate(){
		$id				= $this->input->post('ID');
		$type			= addslashes($this->input->post('type'));
		$code			= $this->input->post('code');
		$callNumber		= $this->input->post('callNumber');
		$title			= addslashes($this->input->post('title'));
		$author			= $this->input->post('author');
		$publisher		= $this->input->post('publisher');
		$year			= $this->input->post('year');
		$available		= $this->input->post('available');

		$sql 			= "";

		if ($id !=  0) {
			$sql = "UPDATE perpustakaan SET 
						type  	  	= '$type',
						code  			= '$code',
						callNumber  = '$callNumber',
						title     	= '$title',
						author     	= '$author',
						publisher  	= '$publisher',
						year     		= '$year',
						available   = '$available'
					WHERE id = '$id' ";
		} else {
	
			$sql = "INSERT INTO perpustakaan(
							type, code, callNumber, title, author, publisher,
							year, available, entrydate)
							
							VALUES (
							'$type', '$code', '$callNumber', '$title', 
							'$author', '$publisher', '$year', '$available',
							now())";
		}

		if (strlen($sql)> 0 ) {
			$this->db->query($sql);			
			$response = "SAVED";
		}
		
		echo $response;
	}	
	/* end here alumni */
		
	
	function pendaftaranExport(){

		$this->data = $this->adminModel->getPendaftaranExport();
		
		$this->smarty->assign('data', $this->data);
		//print_r($this->data);
		$this->smarty->display('admin/pages/pendaftaran_export.tpl');
		//$this->load->view('home/pendaftaran_export', $this->data);
		
	}
	
	function pendaftaranSorting($order='ID', $sort='asc', $page=1, $limit=10){
		$offset = $this->setPaging($page, $limit, $this->adminModel->getPendaftaran());
		$this->data['rows']	 = $this->adminModel->getPendaftaran($order, $sort, $limit, $offset);
		$this->data['order'] = $order;
		$this->data['sort']  = $sort;

		$fields = array('ID', 'no_pendaftaran', 'nama', 'tgl_lahir', 'jenis_kelamin','tinggi_badan', 'tahun_lulus', 'jenis', 'jurusan_smta', 'jurusan', 'lokasiTKD', 'lokasiTKA');
		
		foreach ($fields as $key=>$value) {
			if (strtolower($order) === strtolower($value) ) {
				if ($sort=="none") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="desc") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="asc") {
				   $this->data['sort_'.$value]  = "desc";
				} else {
				   $this->data['sort_'.$value]  = $sort;
				}
				} else {
					 $this->data['sort_'.$value]  = "none";
				}
		}
		
		$this->smarty->assign('data', $this->data);
		$this->smarty->display('admin/pages/pendaftaran_table.tpl');
	}
		
		function pinGenerator(){
			$chars_length	= 6;
			$recordNumber = 6000;
			$recordGenerate = $recordNumber  + 1000;
        
			$characters = array(
			"A","B","C","D","E","F","G","H","J","K","L","M",
			"N","P","Q","R","S","T","U","V","W","X","Y","Z",
			"1","2","3","4","5","6","7","8","9");

			$characters = array("0","1","2","3","4","5","6","7","8","9");
			
			//make an "empty container" or array for our keys
			$pin  = array();
			
			$recordInsert = 0;
						
			for($j=0; $j< $recordGenerate; $j++) {
				$keys = array();
		 		
				//first count of $keys is empty so "1", remaining count is 1-6 = total 7 times
				while(count($keys) < 6  ) {
				  
				    //"0" because we use this to FIND ARRAY KEYS which has a 0 value
				    //"-1" because were only concerned of number of keys which is 32 not 33
				    //count($characters) = 33
				    $x = mt_rand(0, count($characters)-1);
				    if(!in_array($x, $keys)) {
				       $keys[] = $x;
				    }
				}
				$random_chars ="";
				foreach($keys as $key){
				   $random_chars .= $characters[$key];
				}
				//echo "$random_chars = ".strlen($random_chars) ."<br>";			

		    if(!in_array($random_chars, $pin)) {
		    //if(!in_array($random_chars, $pin))  {
		       if ( $recordInsert < $recordNumber ) {
		         $pin[] = $random_chars;

							$sql = "insert into pin(pin) values ($random_chars)";
							//echo "$sql <br>";
							$this->db->query($sql);
							$recordInsert++;
					}
				}

			//}
			
			print_r($pin);
			}		
		}
		
		function pinSorting($order='pinID', $sort='asc', $page=1, $limit=10){
		$offset = $this->setPaging($page, $limit, $this->adminModel->getPin());
		$this->data['rows']	 = $this->adminModel->getPin($order, $sort, $limit, $offset);
		$this->data['order'] = $order;
		$this->data['sort']  = $sort;

		$fields = array('pinID', 'pin', 'state', 'no_pendaftaran');
		foreach ($fields as $key=>$value) {
			if (strtolower($order) === strtolower($value) ) {
				if ($sort=="none") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="desc") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="asc") {
				   $this->data['sort_'.$value]  = "desc";
				} else {
				   $this->data['sort_'.$value]  = $sort;
				}
				} else {
					 $this->data['sort_'.$value]  = "none";
				}
		}
		
		$this->smarty->assign('data', $this->data);
		$this->smarty->display('admin/pages/pin_table.tpl');
	}	
	
		function pinAvailable($order='pinID', $sort='desc', $page=1, $limit=50){
			
		$offset = $this->setPaging($page, $limit, $this->adminModel->getPinAvailable());
		$this->data['rows'] 	= $this->adminModel->getPinAvailable($order, $sort, $limit, $offset);
		$this->data['order']  = $order;
		$this->data['sort']  	= $sort;
		
		

		$fields = array('pin', 'pinID', 'state', 'no_pendaftaran');
		 foreach ($fields as $key=>$value) {
			if (strtolower($order) === strtolower($value) ) {
				if ($sort=="none") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="desc") {
				   $this->data['sort_'.$value]  = "asc";
				} else if  ($sort=="asc") {
				   $this->data['sort_'.$value]  = "desc";
				} else {
				   $this->data['sort_'.$value]  = $sort;
				}
				} else {
					 $this->data['sort_'.$value]  = "none";
				}
		}
		
			//print_r($this->data);
			$this->smarty->assign('data', $this->data);
			$this->smarty->display('admin/pages/pin_table.tpl');
	}
	
		function pinExport($order='pinID', $sort='asc', $page=1, $limit=50, $msg=""){

		$offset = $this->setPaging($page, $limit, $this->adminModel->getPin());
		$this->data['rows'] 	= $this->adminModel->getPin($order, $sort, $limit, $offset);
		$this->data['order']  = $order;
		$this->data['sort']  	= $sort;
		$this->data['page'] 	= $page;
		$this->data['limit'] 	= $limit;
		$this->data['sort_pinID']  = "asc";

		$fields = array('pinID', 'pin', 'state', 'no_pendaftaran');
		  
		foreach ($fields as $key=>$value) {
			$this->data['sort_'.$value]  = "none";
		}
		
		$this->smarty->assign('data', $this->data);
		//print_r($this->data);
		$this->smarty->display('admin/pages/pin_export.tpl');
	}
}